package com.wclass.mapper;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wclass.entity.PostsInfo;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 帖子 Mapper 接口
 * </p>
 *
 * @author SEN
 * @since 2018-12-27
 */
public interface PostsInfoMapper extends BaseMapper<PostsInfo> {


    /**
     * <p>
     * 根据 entity 条件，查询全部记录（并翻页）
     * </p>
     *
     * @param rowBounds 分页查询条件（可以为 RowBounds.DEFAULT）
     * @param wrapper   实体对象封装操作类（可以为 null）
     * @return List<T>
     */
    @Override
    @Select("select info.*,baseAppUser.nick_name,type.name as courseTypeName  ${ew.sqlSelect} from posts_info as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "left join course_type as  type on type.id=info.course_type_id " +
            "where info.state=0 and baseAppUser.state=0 ${ew.sqlSegment}")
    List<PostsInfo> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<PostsInfo> wrapper);

    /**
     * <p>
     * 根据 ID 查询
     * </p>
     *
     * @param id 主键ID
     * @return T
     */
    @Override
    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.level,baseAppUser.postname,type.name as courseTypeName " +
            "from posts_info as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "left join course_type as type on type.id=info.course_type_id " +
            "where info.state=0 and baseAppUser.state=0 and info.id=#{id}")
    PostsInfo selectById(@Param("id") Serializable id);

    /**
     * 更新查看数
     *
     * @param id
     * @return
     */
    @Update("update posts_info set see_num=see_num+1 where id=#{id}")
    Integer updateSeeNum(@Param("id") Serializable id);

    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.level,baseAppUser.postname,type.name as courseTypeName " +
            "from posts_info as info " +
            "left join base_app_user as baseAppUser on info.user_id=baseAppUser.id " +
            "left join course_type as type on type.id=info.course_type_id " +
            "where baseAppUser.state=0 and info.id=#{id}")
    PostsInfo selectContainDelRecord(@Param("id") Serializable id);

}
